﻿(function (FF) {
    //FF.OpenDialog = function (url, windowid, title, width, height, para, maxed) {
    //    var layer = layui.layer;
    //    var index = layer.load(2);
    //    var wid = this.GetCookie("windowids");
    //    var owid = wid;
    //    if (wid === null || wid === '') {
    //        wid = windowid;
    //    }
    //    else {
    //        wid += "," + windowid;
    //    }
    //    this.SetCookie("windowids", wid);
    //    if ("undefined" !== typeof DONOTUSE_WINDOWGUID) {
    //        this.SetCookie("windowguid", DONOTUSE_WINDOWGUID, true);
    //    }
    //    var getpost = "GET";
    //    if (para !== undefined) {
    //        getpost = "Post";
    //    }

    //    $.ajax({
    //        cache: false,
    //        type: getpost,
    //        url: url,
    //        data: para,
    //        async: true,
    //        error: function (xhr) {
    //            layer.close(index);
    //            let location = xhr.getResponseHeader("Location");
    //            if (location) {
    //                window.location = location;
    //                return false;
    //            }
    //            ff.SetCookie("windowids", owid);
    //            if (xhr.responseText !== undefined && xhr.responseText !== "") {
    //                layer.alert(xhr.responseText);
    //            }
    //            else {
    //                layer.alert(ff.DONOTUSE_Text_LoadFailed);
    //            }
    //        },
    //        success: function (str, textStatus, request) {
    //            layer.close(index);
    //            max = true;
    //            if (request.getResponseHeader('IsScript') === 'true') {
    //                ff.SetCookie("windowids", owid);
    //                eval(str);
    //            }
    //            else {
    //                /*str = str.replace(/wtmscript/img, "script");*/
    //                str = "<div  id='" + $.cookie("divid") + "' class='donotuse_pdiv'>" + str + "</div>";
    //                var area = 'auto';
    //                if (width > document.body.clientWidth) {
    //                    max = false;
    //                    maxed = true;
    //                }
    //                if (width !== undefined && width !== null && height !== undefined && height !== null) {
    //                    area = [width + 'px', height + 'px'];
    //                }
    //                if (width !== undefined && width !== null && (height === undefined || height === null)) {
    //                    area = width + 'px';
    //                }
    //                if (title === undefined || title === null || title === '') {
    //                    title = false;
    //                    max = false;
    //                }
    //                var oid = layer.open({
    //                    type: 1
    //                    , title: title
    //                    , area: area
    //                    , maxmin: max
    //                    , offset: '80px'
    //                    , shade: 0.8
    //                    , btn: []
    //                    , id: windowid //设定一个id，防止重复弹出
    //                    , content: str
    //                    , resizing: function (layero) {
    //                        $(layero).find("div[ischart = '1']").each(
    //                            function (index) {
    //                                eval($(this).attr('id') + 'Chart.resize();');
    //                            }
    //                        );
    //                    }
    //                    , full: function (layero) {
    //                        $(layero).find("div[ischart = '1']").each(
    //                            function (index) {
    //                                eval($(this).attr('id') + 'Chart.resize();');
    //                            }
    //                        );
    //                    }
    //                    , restore: function (layero) {
    //                        $(layero).find("div[ischart = '1']").each(
    //                            function (index) {
    //                                eval($(this).attr('id') + 'Chart.resize();');
    //                            }
    //                        );
    //                    }
    //                    , end: function () {
    //                        if (ff.GetCookie("windowids") === wid) {
    //                            ff.SetCookie("windowids", owid);
    //                        }
    //                    }
    //                });
    //                if (maxed === true) {
    //                    layer.full(oid);
    //                }
    //            }
    //        }
    //    });
    //};

    FF.OpenDialog2 = function (url, windowid, title, width, height, tempId, para) {
        var layer = layui.layer;
        var index = layer.load(2);
        var wid = this.GetCookie("windowids");
        var owid = wid;
        if (wid === null || wid === '') {
            wid = windowid;
        }
        else {
            wid += "," + windowid;
        }
        this.SetCookie("windowids", wid);
        this.SetCookie("windowguid", DONOTUSE_WINDOWGUID, true);
        var getpost = "GET";
        if (para !== undefined) {
            getpost = "Post";
        }

        $.ajax({
            cache: false,
            type: getpost,
            url: url,
            data: para,
            async: true,
            error: function (request) {
                layer.close(index);
                ff.SetCookie("windowids", owid);
                if (request.responseText !== undefined && request.responseText !== "") {
                    layer.alert(request.responseText);
                }
                else {
                    layer.alert(ff.DONOTUSE_Text_LoadFailed);
                }
            },
            success: function (str) {
                var regGridId = /<\s{0,}table\s+.*\s+id\s{0,}=\s{0,}"(.*)"\s+lay-filter="\1"\s{0,}.*?>\s{0,}<\s{0,}\/\s{0,}table\s{0,}>/im;
                var regGridVar = /wtVar_(.*)\s{0,}=\s{0,}table.render\([a-zA-Z0-9_]{1,}option\)/im;
                if ($(tempId).length > 0 && regGridId.test(str) && regGridVar.test(str)) {
                    // 获取gridId
                    var gridId = regGridId.exec(str)[1];
                    var gridVar = 'wtVar_' + regGridVar.exec(str)[1];
                    var template = $(tempId)[0].innerHTML;
                    //template = template.replace(/[$]{2}script[$]{2}/img, "<script>").replace(/[$]{2}#script[$]{2}/img, "</script>");
                    //get old gridid
                    try {
                        var oldgridid = /table[.]reloadData\('(.*)',\s{0,}{/img.exec(template)[1];
                        //替换gridId
                        template = template.replace(new RegExp(oldgridid, "gim"), gridId);

                    }
                    catch (e) { }
                    str = str.replace('$$SearchPanel$$', template).replace(",cols", ",size:'sm' ,cols").replace(new RegExp("&amp;#39;", "gim"), "'");
                    str = str.replace('38px', '31px');//修改selector的弹窗表格表头高度
                }
                layer.close(index);
                var area = 'auto';
                if (width !== undefined && width !== null && height !== undefined && height !== null) {
                    area = [width + 'px', height + 'px'];
                }
                if (width !== undefined && width !== null && (height === undefined || height === null)) {
                    area = width + 'px';
                }
                var max = true;
                if (title === undefined || title === null || title === '') {
                    title = false;
                    max = false;
                }
                if (width > document.body.clientWidth) {
                    max = false;
                }
                var oid = layer.open({
                    type: 1
                    , title: title
                    , area: area
                    , maxmin: max
                    , btn: []
                    , shade: 0.3
                    , id: windowid //设定一个id，防止重复弹出
                    , content: str
                    , end: function () {
                        ff.SetCookie("windowids", owid);
                    }
                });
                if (width > document.body.clientWidth) {
                    layer.full(oid);
                }

            }
        });
    };


    FF.LoadComboItems = function (controltype, url, controlid, targetname, svals, cb) {
        var target = $("#" + controlid);
        var targetfilter = target.attr("lay-filter");
        var ismulticombo = target.attr("wtm-combo") != undefined;
        if (svals == undefined || svals == null) {
            svals = [];
        }
        $.get(url, {}, function (data, status) {
            if (status === "success") {
                var i = 0;
                var item = null;
                if (controltype === "tree") {
                    var da = ff.getTreeItems(data.Data, svals);
                    layui.tree.reload(controlid, {
                        data: da
                    });
                    if (cb !== undefined && cb != null) {
                        cb();
                    }
                }
                if (controltype == "transfer") {
                    layui.transfer.reload(controlid, {
                        data: ff.getTransferItems(data.Data, svals)
                    });
                }
                if (controltype === "combo") {

                    target.html('<option value = ""  selected>' + ff.DONOTUSE_Text_PleaseSelect + '</option>');
                    var arr = [];
                    if (data.Data !== undefined && data.Data !== null) {
                        for (i = 0; i < data.Data.length; i++) {
                            item = data.Data[i];
                            var icon = item.Icon !== undefined && item.Icon != null && item.Icon.length > 0 ? ' icon="' + item.Icon + '"' : '';
                            if (item.Selected === true || svals.indexOf(item.Value.toLowerCase()) > -1) {
                                target.append('<option value = "' + item.Value + '"' + icon + ' selected>' + item.Text + '</option>');
                                arr.push({ name: item.Text, val: item.Value });
                            }
                            else {
                                target.append('<option value = "' + item.Value + '" ' + icon + '>' + item.Text + '</option>');
                            }
                        }
                    }
                    layui.form.render('select', targetfilter + "div");
                    if (ismulticombo) {
                        layui.use(['form', 'formSelects'], function () {
                            var mm = layui.formSelects.selects[targetfilter];
                            ff.refreshcombobox(mm, arr);
                        });
                    }
                }
                if (controltype === "checkbox") {
                    for (i = 0; i < data.Data.length; i++) {
                        item = data.Data[i];
                        if (item.Selected === true || svals.indexOf(item.Value) > -1) {
                            target.append("<input type='checkbox'  name = '" + targetname + "' value = '" + item.Value + "' title = '" + item.Text + "' checked />");
                        }
                        else {
                            target.append("<input type='checkbox' name = '" + targetname + "' value = '" + item.Value + "' title = '" + item.Text + "'  />");
                        }
                    }
                    layui.form.render('checkbox', targetfilter + "div");
                }
                if (controltype === "radio") {
                    for (i = 0; i < data.Data.length; i++) {
                        item = data.Data[i];
                        if (item.Selected === true || svals.indexOf(item.Value) > -1) {
                            target.append("<input type='radio'  name = '" + targetname + "' value = '" + item.Value + "' title = '" + item.Text + "' checked />");
                        }
                        else {
                            target.append("<input type='radio' name = '" + targetname + "' value = '" + item.Value + "' title = '" + item.Text + "'  />");
                        }
                    }
                    layui.form.render('radio', targetfilter + "div");
                }

            }

            else {
                layer.alert(ff.DONOTUSE_Text_FailedLoadData);
            }
        });

    };

    FF.AddGridRow = function (gridid, data) {
        var loaddata = layui.table.cache[gridid];
        for (val in data) {
            if (val.toUpperCase() === "ID") {
                data[val] = ff.guid();
            }
        }
        var re = /(<input .*?)\s*\/>(.*?)/ig;
        var re2 = /(<select .*?)\s*>(.*?<\/select>)/ig;
        var re3 = /(.*?)<input hidden name='(.*?)\.id' .*?\/>(.*?)/ig;
        for (val in data) {
            if (typeof (data[val]) == 'string') {
                data[val] = data[val].replace(/\[\d?\]/ig, "[" + loaddata.length + "]");
                data[val] = data[val].replace(/_\d?_/ig, "_" + loaddata.length + "_");
                data[val] = data[val].replace(re, "$1 onchange=\"ff.gridcellchange(this,'" + gridid + "'," + loaddata.length + ",'" + val + "',0)\" />$2");
                data[val] = data[val].replace(re2, "$1 onchange=\"ff.gridcellchange(this,'" + gridid + "'," + loaddata.length + ",'" + val + "',1)\" >$2");
                data[val] = data[val].replace(re3, "$1 <input hidden name='$2.id' value='" + data["ID"] + "'/> $3");
            }
        }
        loaddata.push(data);

        layui.table.reload(gridid, {
            url: null,
            data: loaddata,
            limit: 9999
        });
    };

    FF.RemoveGridRow = function (gridid, index) {
        var loaddata = layui.table.cache[gridid];
        loaddata.splice(index - 1, 1);
        for (var i = 0; i < loaddata.length; i++) {
            for (val in loaddata[i]) {
                if (typeof (loaddata[i][val]) == 'string') {
                    loaddata[i][val] = loaddata[i][val].replace(/\[\d?\]/ig, "[" + i + "]");
                    loaddata[i][val] = loaddata[i][val].replace(/_\d?_/ig, "_" + i + "_");
                    loaddata[i][val] = loaddata[i][val].replace("/onchange=\".*?\"/", "onchange=\"ff.gridcellchange(this,'" + gridid + "'," + i + ",'" + val + "')\"");
                }
            }
        }

        layui.table.reload(gridid, {
            url: null,
            data: loaddata,
            limit: 9999
        });
    };

})(ff);